package com.zh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zh.dto.DishDto;
import com.zh.dto.SetmealDto;
import com.zh.entity.Dish;
import com.zh.entity.Setmeal;
import com.zh.entity.SetmealDish;
import com.zh.mapper.SetmealMapper;
import com.zh.service.DishService;
import com.zh.service.SetmealDishService;
import com.zh.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author 你爹
 * @since 2023-09-21
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    @Transactional
    @Override
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public void saveWithDishes(SetmealDto setmealDto) {
        log.info("保存套餐信息:{}", setmealDto);
        //存setmeal
        this.save(setmealDto);
        //存setmeal_dishes
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes().stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId().toString());
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishList);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public void updateStatus(Integer status, List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            this.updateById(setmeal);
        });
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public void deleteWithDishes(List<Long> ids) {
        //先删除套餐
        this.removeByIds(ids);
        //再删套餐-菜品
        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishService.remove(wrapper);
    }

    @Override
    public SetmealDto getSetmealById(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        //查套餐对应的菜品
        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(setmealDishList);
        return setmealDto;
    }

    @Override
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    public void updateWithDishes(SetmealDto setmealDto) {
        //先更新setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDto, setmeal);
        this.updateById(setmeal);
        //再更新setmealDish
        Long setmealId = setmeal.getId();
        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealId);
        setmealDishService.remove(wrapper);

        if (setmealDto.getSetmealDishes() != null && setmealDto.getSetmealDishes().size() > 0) {
            List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes().stream().peek(
                    setmealDish ->
                            setmealDish.setSetmealId(setmealId.toString())).collect(Collectors.toList());
            setmealDishService.saveBatch(setmealDishList);
        }
    }

    @Override
    @Cacheable(cacheNames = "setmeal", key = "'_dish_'+#setmealId")
    public List<DishDto> getOneSetmeal(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishList = setmealDishService.list(wrapper);
        List<DishDto> dishDtoList = new ArrayList<>();
        setmealDishList.stream().peek(setmealDish -> {
            DishDto dishDto = new DishDto();
            Dish dish = dishService.getById(setmealDish.getDishId());
            BeanUtils.copyProperties(dish, dishDto);
            dishDto.setPrice(setmealDish.getPrice());
            dishDto.setCopies(setmealDish.getCopies());
            dishDtoList.add(dishDto);
        }).collect(Collectors.toList());
        return dishDtoList;
    }
}
